package cn.gailvlun.gll.presentation.emotion.core

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.MotionEvent
import android.view.View
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityImmersionHeaderBinding
import cn.gailvlun.gll.net.emotion.RefreshReq
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.RefreshState
import cn.gailvlun.gll.presentation.emotion.util.BarBuilder
import cn.gailvlun.gll.util.DensityUtil
import cn.gailvlun.gll.widget.NavigationBarView
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener
import java.util.*


abstract class ImmersionHeaderActivity : BaseActivity() {
    protected lateinit var binding: ActivityImmersionHeaderBinding
     lateinit var adapter: BaseAdapter<*>
     lateinit var viewModel: BaseViewModel
    protected lateinit var linearLayoutManager: LinearLayoutManager
     lateinit var req: RefreshReq
    var randon = Random()
    var randon1 = Random()
    var randon2 = Random()

    abstract fun initAdapter(): BaseAdapter<*>
    abstract fun initViewModel(): BaseViewModel
    open fun initRequest(): RefreshReq {
        return RefreshReq()
    }

    enum class SubclassEnum {
        Square,
        Personal,
        SameList
    }

    abstract fun getSubclassEnum(): SubclassEnum
    override fun initComponent() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_immersion_header)
        adapter = initAdapter()
        viewModel = initViewModel()
        req = initRequest()
        adapter.setPresenter(viewModel)
        linearLayoutManager = LinearLayoutManager(mContext)
        binding.rvEmotionList.adapter = adapter
        binding.rvEmotionList.layoutManager = linearLayoutManager
    }


    protected var state = RefreshState.INIT
    override fun createHandler() {
        binding.nbv.setOnMenuClickListener { nbv ->
            onMenuClick(nbv)
        }
        binding.nbv.setOnBackListener { onBackPressed() }
        binding.nbv.setOnDoubleClickListener(
                { binding.rvEmotionList.smoothScrollToPosition(0) })
        binding.srl.setOnRefreshLoadmoreListener(
                object : OnRefreshLoadmoreListener {
                    override fun onLoadmore(refreshlayout: RefreshLayout) {
                        this@ImmersionHeaderActivity.onLoadmore(refreshlayout)
                    }

                    override fun onRefresh(refreshlayout: RefreshLayout) {
                        this@ImmersionHeaderActivity.onRefresh(refreshlayout)
                    }
                })

        binding.rvEmotionList.addOnScrollListener(
                object : RecyclerView.OnScrollListener() {
                    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                        super.onScrolled(recyclerView, dx, dy)
                        this@ImmersionHeaderActivity.onScroll(recyclerView, dx, dy)

                    }
                })

        binding.srl.setOnMultiPurposeListener(
                object : SimpleMultiPurposeListener() {
                    override fun onHeaderPulling(header: RefreshHeader?, percent: Float, offset: Int, headerHeight: Int, extendHeight: Int) {
                        super.onHeaderPulling(header, percent, offset, headerHeight, extendHeight)
                        this@ImmersionHeaderActivity.onHeaderPulling(header, percent, offset, headerHeight, extendHeight)
                        state = RefreshState.PULLING
                    }

                    override fun onHeaderReleasing(header: RefreshHeader?, percent: Float, offset: Int, headerHeight: Int, extendHeight: Int) {
                        super.onHeaderReleasing(header, percent, offset, headerHeight, extendHeight)
                        this@ImmersionHeaderActivity.onHeaderReleasing(header, percent, offset, headerHeight, extendHeight)
                    }

                    override fun onHeaderReleased(header: RefreshHeader?, headerHeight: Int, extendHeight: Int) {
                        super.onHeaderReleased(header, headerHeight, extendHeight)
                        state = RefreshState.RELEASED
                    }

                    override fun onHeaderFinish(header: RefreshHeader?, success: Boolean) {
                        super.onHeaderFinish(header, success)
                        state = RefreshState.COMPLETE
                    }
                })
    }

    protected fun autoRefresh() {
        binding.srl.autoRefresh()
    }

    open fun getHeaderHeight(): Float {
        return if (SubclassEnum.SameList == getSubclassEnum()) 200f else 343f
    }

    open fun onScroll(recyclerView: RecyclerView, dx: Int, dy: Int) {
        val height = DensityUtil.dp2px(mContext, getHeaderHeight()) - binding.nbv.height
        val scrollY = recyclerView.computeVerticalScrollOffset()
        if (scrollY > height) {
            when (getSubclassEnum()) {
                SubclassEnum.Square -> {
                    adapter(
                            R.color.white, "情绪博物馆",
                            R.drawable.icon_emotion_square_title_menu, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_scroll, R.color.emotion_dark
                    )
                }
                SubclassEnum.SameList -> {
                    adapter(
                            R.color.white, "情绪签",
                            R.drawable.icon_emotion_square_title_menu, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_default, R.color.emotion_dark
                    )
                }
                SubclassEnum.Personal -> {
                    adapter(
                            R.color.white, "我的情绪",
                            null, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_scroll, R.color.emotion_dark
                    )
                }

            }
        } else {
            when (getSubclassEnum()) {
                SubclassEnum.Square -> {
                    adapter(
                            R.color.white, "情绪博物馆",
                            R.drawable.icon_emotion_square_title_menu, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_default, R.color.transparent
                    )
                }

                SubclassEnum.SameList -> {
                    adapter(
                            R.color.white, "情绪签",
                            R.drawable.icon_emotion_square_title_menu, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_default, R.color.transparent
                    )
                }
                SubclassEnum.Personal -> {
                    adapter(
                            R.color.white, "我的情绪",
                            null, NavigationBarView.MenuType.ICON,
                            R.drawable.nbv_emotion_back_default, R.color.transparent
                    )
                }
            }
        }
    }

    fun adapter(@ColorRes titleColor: Int = R.color.black, titleText: String,
                @DrawableRes menuIcon: Int?, menuType: NavigationBarView.MenuType,
                @DrawableRes backIcon: Int, @ColorRes fakeStatusBarBgColor: Int) {
        BarBuilder(binding.flFakeStatusBar).change {
            title {
                this.titleColor = titleColor
                this.titleText = titleText
                this.alpha = 1f
                this.visibility = View.VISIBLE
            }
            menu {
                if (menuIcon != null){
                    this.menuIcon = menuIcon
                }
            }
            back { this.backIcon = backIcon }
            this.fakeStatusBarBgColor = fakeStatusBarBgColor
        }
    }


    open fun onLoadmore(refreshlayout: RefreshLayout) {
        req.isLoadMore = true
        getData(req)

    }

    open fun onRefresh(refreshlayout: RefreshLayout) {
        req.isLoadMore = false
        getData(req)
    }

    open fun onHeaderPulling(header: RefreshHeader?, percent: Float, offset: Int, headerHeight: Int, extendHeight: Int) {
    }

    open fun onHeaderReleasing(header: RefreshHeader?, percent: Float, offset: Int, headerHeight: Int, extendHeight: Int) {

    }

    open fun onMenuClick(nbv: NavigationBarView) {

    }

    override fun loadData(savedInstanceState: Bundle?) {
        getData(req)
    }

    open fun getData(req: RefreshReq) {

    }


    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return if (viewModel.functionPopupWindow!!.isShowing) {
            viewModel.functionPopupWindow!!.dismiss()
            true
        } else {
            super.dispatchTouchEvent(ev)
        }
    }

}